      SUBROUTINE QUIKVIS5U
      IMPLICIT REAL*8 (A-H,O-Z)
C
C THIS ROUTINE IS PART OF THE QUIKVIS PROGRAM.  IT WRITES DATA TO AND
C READS DATA FROM THE SCRATCH DATA FILE, LUSCR1.  READ/WRITE IS ISOLATED
C IN THIS ROUTINE SO THAT FUTURE MODS INVOLVING THE SCRATCH FILE WILL
C BE MORE EASILY IMPLEMENTED.
C
C THERE ARE TWO ENTRY POINTS, QV5READ FOR READING AND QV5WRITE FOR
C WRITING.
C
C***********************************************************************
C
C BY C PETRUZZO/GFSC/742.   9/86.
C       MODIFIED....
C
C***********************************************************************
C
      INCLUDE 'QUIKVIS.INC'
C
      CHARACTER*16 TARGNAME
      REAL*8 VISTIMES(MAXNODES),EVENTMA(MAXNODES),
     *       REQMTMA(2,MAXREQMT,MAXNODES)
      REAL*8 XVISTIMES(MAXNODES),XEVENTMA(MAXNODES),
     *       XREQMTMA(2,MAXREQMT,MAXNODES)
      LOGICAL EVLOAD,VISLOAD,REQLOAD
      INTEGER J1REC/99999/
      INTEGER IBUG/ 0/,LUBUG/19/
C
C
C
C-----------------------  ENTRY = QV5WRITE  ----------------------------
C
C WRITE DATA ON LUSCR1.
C
      ENTRY QV5WRITE(KREC,T50,ITARG,ID,TARGNAME,FIRSTNODE,DELNODE,
     *        FIRSTMST,DELMST,NUMNODES,EVENTMA,VISTIMES,REQMTMA)
C
C VARIABLE   DIM  TYPE  I/O  DESCRIPTION
C --------   ---  ----  ---  -----------
C
C KREC        1    I*4   I   THE RECORD NUMBER THE CALLER EXPECTS TO
C                            WRITE.  IF =1, THEN A REWIND OCCURS BEFORE
C                            THE RECORD IS WRITTEN.  IF NOT 1, NO REWIND
C                            OCCURS.
C
C                            AN INTERNAL COUNTER KEEPS TRACK OF THE
C                            RECORD NUMBER AND IF IT DISAGREES WITH WHAT
C                            THE CALLER EXPECTS, AN ERROR END OCCURS IN
C                            THIS ROUTINE.
C
C                            THIS ROUTINE ASSUMES THAT KREC'S WILL BE
C                            ENTERED IN THE SEQUENCE 1,2,3,....,N. IE,
C                            NO GAPS, NO DUPLICATES, IN ASCENDING ORDER.
C
C T50         1    R*8   I   THE TIME ASSOCIATED WITH THIS RECORD.  IN
C                            SECONDS SINCE 1/1/50, 0.0 HRS.
C
C                            IF SUN AND/OR TARGET POSITION WAS INVOLVED
C                            IN COMPUTING VISTIMES(BELOW), THEN T50 IS
C                            THE TIME AT WHICH THE POSITIONS WERE
C                            USED.
C
C ITARG       1    I*4   I   A COUNTER.  THIS IS THE TARGET NUMBER IN
C                            THE SET OF TARGETS BEING PROCESSED.
C
C ID          1    I*4   I   THE TARGET ID.  THE SAME ID THAT IS IN THE
C                            TARGET CATALOGUE.
C
C TARGNAME    1   CH*16  I   THE TARGET NAME.  SAME NAME THAT IS IN THE
C                            TARGET CATALOGUE.
C
C FIRSTNODE   1    R*8   I   THE FIRST RAAN IN THE SEQUENCE OF RAANS
C                            BEING PROCESSED.  IN RADIANS.
C
C DELNODE     1    R*8   I   THE RAAN INCREMENT.  IN RADIANS.
C
C FIRSTMST    1    R*8   I   THE MEAN SOLAR TIME ASSOCIATED WITH THE
C                            FIRST RAAN(=FIRSTNODE).  IN SECONDS SINCE
C                            MIDNIGHT.
C
C DELMST      1    R*8   I   THE MEAN SOLAR TIME INCREMENT.  IN SECONDS.
C
C NUMNODES    1    I*4   I   THE NUMBER OF RAAN'S PROCESSED.
C
C EVENTMA     1    I*4   I   THE MEAN ANOMALY AT WHICH THE ORBIT EVENT
C                            OCCURS.  IN RADIANS.
C
C                            MEAN ANOMALY AND ELAPSED TIME ARE MEASURED
C                            RELATIVE TO THIS EVENT.  QUIKVIS5A COMPUTES
C                            EVENTMA.
C
C VISTIMES         I*4   I   VISTIMES(I) IS THE DURATION PER ORBIT THAT
C         MAXNODES           THE OBSERVATION REQUIREMENTS ARE SATISFIED
C                            FOR THE I'TH RAAN VALUE.  IN SECONDS.
C
C REQMTMA          R*8   I   REQMTMA(1,IREQ,INODE) IS THE MEAN ANOMALY
C    2,MAXREQMT,MAXNODES     WHERE AVAILABILITY BEGINS FOR THE IREQ'TH
C                            REQUIREMENT AND THE INODE'TH NODE;
C                            REQMTMA(2,IREQ,INODE) IS WHERE IT ENDS.
C
C                            QUIKVIS5A LOADS REQMTMA ONLY UNDER CERTAIN
C                            CONDITIONS(SEE NEEDALL VARIABLE THERE)
C
C
C
      IF(KREC.EQ.1) THEN
        J1REC = 0
        REWIND LUSCR1
        END IF
C
      J1REC = J1REC + 1
C
      IF(J1REC.NE.KREC) THEN
C      CODING ERROR. SEE KREC COMMENTS ABOVE.
        STOP ' QUIKVIS5U. CODING ERROR 1. STOPPED.'
        END IF
C
      WRITE(LUSCR1)
     *    J1REC,   ! THE RECORD NUMBER BEING WRITTEN.
     *    T50,
     *    ITARG,
     *    ID,
     *    TARGNAME,
     *    FIRSTNODE,DELNODE,FIRSTMST,DELMST,NUMNODES,
     *    (EVENTMA(I),I=1,NUMNODES),
     *    (VISTIMES(I),I=1,NUMNODES),
     *    (((REQMTMA(I,J,K),I=1,2),J=1,MAXREQMT),K=1,NUMNODES)
C
      IF(IBUG.NE.0) THEN
        WRITE(LUBUG,6601) IFLAG,J1REC,ID,PAKTIM50(T50),NUMNODES,
     *      FIRSTNODE*DEGRAD,DELNODE*DEGRAD,
     *      FIRSTMST/3600.D0,DELMST/3600.D0
        WRITE(LUBUG,6602)
     *      (I, (FIRSTNODE+(I-1)*DELNODE)*DEGRAD,
     *         EVENTMA(I)*DEGRAD, VISTIMES(I)/60.D0, I=1,NUMNODES)
        END IF
C
      RETURN
C
C
C
C-----------------------  ENTRY = QV5READ  -----------------------------
C
C READ DATA FROM LUSCR1.
C
      ENTRY QV5READ(KREC,
     *       T50,ITARG,ID,TARGNAME,
     *       FIRSTNODE,DELNODE,FIRSTMST,DELMST,NUMNODES,
     *       EVLOAD,EVENTMA, VISLOAD,VISTIMES, REQLOAD,REQMTMA)
C
C
C VARIABLE   DIM  TYPE  I/O  DESCRIPTION
C --------   ---  ----  ---  -----------
C
C KREC        1    I*4   I   THE RECORD NUMBER THE CALLER EXPECTS TO
C                            READ.  IF =1, THEN A REWIND OCCURS BEFORE
C                            THE FILE IS READ.  IF NOT 1, NO REWIND
C                            OCCURS.
C
C                            THIS ROUTINE ASSUMES THAT THE CALLER WILL
C                            WANT TO READ THE RECORDS SEQUENTIALLY. EACH
C                            RECORD CONTAINS ITS RECORD NUMBER AND IF
C                            THE RECORD NUMBER READ DOES NOT AGREE WITH
C                            THE NUMBER EXPECTED(IE, KREC), A CODING
C                            ERROR EXISTS AND THE RUN STOPS IN THIS
C                            ROUTINE.
C
C T50         1    R*8   O   READ FROM LUSCR1; SAME DESCRIPTION AS ABOVE
C
C ITARG       1    I*4   O   READ FROM LUSCR1; SAME DESCRIPTION AS ABOVE
C
C ID          1    I*4   O   READ FROM LUSCR1; SAME DESCRIPTION AS ABOVE
C
C TARGNAME    1   CH*16  O   READ FROM LUSCR1; SAME DESCRIPTION AS ABOVE
C
C FIRSTNODE   1    R*8   O   READ FROM LUSCR1; SAME DESCRIPTION AS ABOVE
C
C DELNODE     1    R*8   O   READ FROM LUSCR1; SAME DESCRIPTION AS ABOVE
C
C FIRSTMST    1    R*8   O   READ FROM LUSCR1; SAME DESCRIPTION AS ABOVE
C
C DELMST      1    R*8   O   READ FROM LUSCR1; SAME DESCRIPTION AS ABOVE
C
C NUMNODES    1    I*4   O   READ FROM LUSCR1; SAME DESCRIPTION AS ABOVE
C
C EVLOAD      1    L*4   I   FLAG INDICATING WHETHER EVENTMA IS TO BE
C                            LOADED;  =.TRUE., LOAD IT;
C
C EVENTMA MAXNODES I*4   O   READ FROM LUSCR1; SAME DESCRIPTION AS ABOVE
C
C                            IF EVLOAD=.FALSE., A DUMMY VARIABLE MAY
C                            BE USED.
C
C VISLOAD     1    L*4   I   FLAG INDICATING WHETHER VISTIMES IS TO BE
C                            LOADED;  =.TRUE., LOAD IT
C
C VISTIMES         I*4   O   READ FROM LUSCR1; SAME DESCRIPTION AS ABOVE
C         MAXNODES
C
C                            IF VISLOAD=.FALSE., A DUMMY VARIABLE MAY
C                            BE USED.
C
C REQLOAD     1    L*4   I   FLAG INDICATING WHETHER REQMTMA IS TO BE
C                            LOADED;  =.TRUE., LOAD IT
C
C REQMTMA          R*8   O   READ FROM LUSCR1; SAME DESCRIPTION AS ABOVE
C    2,MAXREQMT,MAXNODES
C                            IF REQLOAD=.FALSE., A DUMMY VARIABLE MAY
C                            BE USED.
C
C
      IF(KREC.EQ.1) REWIND LUSCR1
C
      READ(LUSCR1)
     *    J2REC,
     *    T50,
     *    ITARG,
     *    ID,
     *    TARGNAME,
     *    FIRSTNODE,DELNODE,FIRSTMST,DELMST,NUMNODES,
     *    (XEVENTMA(I),I=1,NUMNODES),
     *    (XVISTIMES(I),I=1,NUMNODES),
     *    (((XREQMTMA(I,J,K),I=1,2),J=1,MAXREQMT),K=1,NUMNODES)
C
      IF(IBUG.NE.0) THEN
        WRITE(LUBUG,7601) KREC,J2REC,ID,PAKTIM50(T50),NUMNODES,
     *      FIRSTNODE*DEGRAD,DELNODE*DEGRAD,
     *      FIRSTMST/3600.D0,DELMST/3600.D0
        WRITE(LUBUG,7602)
     *      (I, (FIRSTNODE+(I-1)*DELNODE)*DEGRAD,
     *         XEVENTMA(I)*DEGRAD, XVISTIMES(I)/60.D0, I=1,NUMNODES)
        END IF
C
      IF(J2REC.NE.KREC) THEN
C      CODING ERROR. SEE KREC COMMENTS ABOVE.
        STOP ' QUIKVIS5U. CODING ERROR 2. STOPPED.'
        END IF
C
      IF(EVLOAD)  CALL MTXEQL(XEVENTMA,EVENTMA,1,NUMNODES)
C
      IF(VISLOAD) CALL MTXEQL(XVISTIMES,VISTIMES,1,NUMNODES)
C
      IF(REQLOAD) CALL MTXEQL(XREQMTMA,REQMTMA,2*MAXREQMT,NUMNODES)
C
C    RESET J1REC TO ENSURE THAT A SUBSEQUENT WRITE VIA QV5WRITE CANNOT
C    OCCUR WITHOUT KREC=1 BEING ENTERED THERE.
      J1REC = 99999
C
      RETURN
C
C***********************************************************************
C
C
C**** INITIALIZATION CALL. PUT GLOBAL PARAMETER VALUES INTO THIS
C     ROUTINE'S LOCAL VARIABLES.
C
      ENTRY QVINIT5U
C
      CALL QUIKVIS999(-1,R8DATA,I4DATA,L4DATA)
      RETURN
C
C***********************************************************************
C
 6601 FORMAT(/,
     *    ' QUIKVIS5U.  DEBUG 6601 AND 6602. SCRATCH FILE WRITTEN.',
     *         '   IFLAG =',I5/,
     *    '   J1REC=',I3,'  TARG ID=',I5,'  TIME=',F13.6,
     *            '  NUMNODES=',I4/,
     *    '   FIRSTNODE=',F8.2,' INCR=',F8.2,
     *        '   FIRSTMST=',F7.3,' INCR=',F7.3)
 6602 FORMAT(
     *    1X,I3,'  RAAN=',F8.2,'  EVENTMA=',F8.2,'  VISTIMES=',G13.5)
 7601 FORMAT(/,
     *    ' QUIKVIS5U.  DEBUG 7601 AND 7602. SCRATCH FILE READ.',
     *         '  KREC=',I5/,
     *    '   J2REC=',I3,'  TARG ID=',I5,'  TIME=',F13.6,
     *            '  NUMNODES=',I4/,
     *    '   FIRSTNODE=',F8.2,' INCR=',F8.2,
     *        '   FIRSTMST=',F7.3,' INCR=',F7.3)
 7602 FORMAT(
     *    1X,I3,'  RAAN=',F8.2,'  XEVENTMA=',F8.2,'  XVISTIMES=',G13.5)
C
      END
